/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.snda.lidejia.jobflow.task;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author lidejia
 * 
 * 
 * the input or output of each task
 */
public class TaskData<T> implements Iterable<T> {

    private List<T> datas = new LinkedList<T>();

    public void addData(T data) {
        datas.add(data);
    }

    public int size() {
        return datas.size();
    }

    public List<TaskData<T>> split(int number) {
        // the actual number to split
        number = Math.min(number, datas.size());
        List<TaskData<T>> splits = new ArrayList<TaskData<T>>(number);
        // init the splits
        for (int i = 0; i < number; i++) {
            splits.add(new TaskData<T>());
        }
        // random split
        int index = 0;
        for (T data : datas) {
            TaskData<T> taskData = splits.get(index);
            taskData.addData(data);
            index = (index + 1) % number;
        }
        return splits;
    }

    public void merge(TaskData<T> taskData) {
        datas.addAll(taskData.datas);
    }

    @Override
    public Iterator<T> iterator() {
        return datas.iterator();
    }
}
